package com.ocom.wxapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.CompanyClientService;
import com.ocom.wxapi.client.HumanClientService;
import com.ocom.wxapi.controller.MainPageController;
import com.ocom.wxapi.service.MainPageService;
import com.ocom.wxapi.vo.YkmNumsVo;
import com.ocom.wxapi.vo.YkmSumByHSVo;
import com.ocom.wxapi.vo.YkmSumByYMVo;
import com.ocom.wxapi.vo.YkmSumListVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MainPageServiceImpl implements MainPageService {
    private final Logger log = LoggerFactory.getLogger(MainPageController.class);



    @Autowired
    private AccessClientService accessClientService;

    @Autowired
    private HumanClientService humanClientService;

    /**
     * 防疫信息页面（MainPage/onYKMShow）
     * */
    @Override
    public Result onYKMShow(Long comId, Long deptId) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),comId);
        return accessClientService.getYkmPersonnelSum(map);
    }

    /**
     * 防疫汇总指标列表（MainPage/onYkmSum）
     * */
    @Override
    public Result onYkmSum(YkmSumListVo ykmSumListVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();

        if (ykmSumListVo.getDeptDown().equals(0)){
            map.put(mapKey.getKey_comId(),ykmSumListVo.getComId());
            if (ykmSumListVo.getDeptId()!=null && ykmSumListVo.getDeptId()>-1){
                map.put(mapKey.getKey_deptIds(),ykmSumListVo.getDeptId());
            }
            map.put(mapKey.getKey_pName(),ykmSumListVo.getPName());
            map.put(mapKey.getKey_pCode(),ykmSumListVo.getPCode());
            map.put(mapKey.getKey_paraSum(),ykmSumListVo.getParaSum());
            map.put(mapKey.getKey_page(),ykmSumListVo.getPage());
            map.put(mapKey.getKey_size(),ykmSumListVo.getSize());
            log.info("防疫汇总指标列表:map{{}}",map);
          return   accessClientService.getYkmSumList(map);
        }else {
            if(ykmSumListVo.getDeptId()!=null && ykmSumListVo.getDeptId()>-1){
                Map<String, Object> map1 = new HashMap<>();

                map1.put(mapKey.getKey_comId(),ykmSumListVo.getComId());
                map1.put(mapKey.getKey_deptId(),ykmSumListVo.getDeptId());
                map1.put(mapKey.getKey_showLevel(),0);
                log.info("防疫汇总指标列表:map1{{}}",map1);
                Result result = humanClientService.getDeptIds(map1);
                if (result.getCode()!=200){
                    return ResultUtil.error(result.getCode());
                }
                log.info("防疫汇总指标列表部门id:result{{}}",result);
                map.put(mapKey.getKey_deptIds(),JSON.parseObject(JSON.toJSONString(result.getData())).getString("deptIds")+","+ykmSumListVo.getDeptId());
            }

            map.put(mapKey.getKey_comId(),ykmSumListVo.getComId());

            map.put(mapKey.getKey_pName(),ykmSumListVo.getPName());
            map.put(mapKey.getKey_pCode(),ykmSumListVo.getPCode());
            map.put(mapKey.getKey_paraSum(),ykmSumListVo.getParaSum());
            map.put(mapKey.getKey_page(),ykmSumListVo.getPage());
            map.put(mapKey.getKey_size(),ykmSumListVo.getSize());
            log.info("防疫汇总指标列表:map{{}}",map);
            return  accessClientService.getYkmSumList(map);
        }
    }
}
